package com.backend.demo.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.RequestAware;
import org.springframework.stereotype.Controller;

import com.backend.demo.entities.EnumSort;
import com.backend.demo.service.IEnumSortService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 枚举Action
 * @author Administrator
 *
 */
@ParentPackage("user")
@Namespace("/jspView")
@InterceptorRefs(value = { @InterceptorRef(value = "loginStack") })
@Controller
public class EnumSortAction extends ActionSupport implements 
		ModelDriven<EnumSort>, RequestAware {

	private static final long serialVersionUID = 1L;
	
	private Map<String,Object> request;
	
	private EnumSort enumSort;
	
	@Resource(name="enumSortService")
	private IEnumSortService enumSortService;
	
	public void setEnumSort(EnumSort enumSort) {
		this.enumSort = enumSort;
	}
	
	public EnumSort getEnumSort() {
		return enumSort;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	@Override
	public EnumSort getModel() {
		if(enumSort == null){
			enumSort = new EnumSort();
		}
		return enumSort;
	}
	
	@Action(value="getAllEnumSorts",results={@Result(name=SUCCESS,location="/jspView/dict/enumSort/enumSortList.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getAllEnumSorts(){
		List<EnumSort> enumSorts = enumSortService.getAllEnumSorts();
		request.put("enumSorts", enumSorts);
		return SUCCESS;
	}
	
	@Action(value="getEnumSortsByCondition",results={@Result(name=SUCCESS,location="/jspView/dict/enumSort/enumSortList.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getEnumSortsByCondition(){
		List<EnumSort> enumSorts = enumSortService.getEnumSortsByCondition(enumSort);
		request.put("enumSorts", enumSorts);
		return SUCCESS;
	}
	
	@Action(value="getEnumSortByEnumSortID",results={@Result(name=SUCCESS,location="/jspView/dict/enumSort/editEnumSort.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getEnumSortByEnumSortID(){
		EnumSort enumSortNew = enumSortService.getEnumSortById(enumSort.getEnumsortid());
		request.put("enumSort", enumSortNew);
		return SUCCESS;
	}
	
	@Action(value="addEnumSort",results={@Result(name=SUCCESS,location="/jspView/getAllEnumSorts"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String addEnumSort(){
		int num = enumSortService.addEnumSort(enumSort);
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="editEnumSort",results={@Result(name=SUCCESS,location="/jspView/getAllEnumSorts"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String editEnumSort(){
		int num = enumSortService.editEnumSort(enumSort);
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="deleteEnumSortByEnumSortID",results={@Result(name=SUCCESS,location="/jspView/getAllEnumSorts"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String deleteEnumSortByEnumSortID(){
		int num = enumSortService.deleteEnumSortByEnumSortID(enumSort.getEnumsortid());
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="redirectEnumSort",results={@Result(name=SUCCESS,location="/jspView/dict/enumSort/addEnumSort.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String redirectEnumSort(){
		return SUCCESS;
	}

}
